package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;

import Database.StaffDB;
import Model.Approval;
import Model.Committee;
import Model.CommitteeRole;

/**
 * Servlet implementation class RoleTable
 */

public class CommitteePendingTable extends HttpServlet {
	private static final long serialVersionUID = 1L;
    JspWriter out;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommitteePendingTable() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public CommitteePendingTable(JspWriter outputStream) {
		// TODO Auto-generated constructor stub
		out = outputStream;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession();
		
		String username = (String) session.getAttribute("username");
		String userrole = (String) session.getAttribute("role");
		ArrayList<Committee> commTable = new ArrayList<Committee>();
		commTable = Database.CommitteeDB.getAllCommitteesUnApproved();
		ArrayList<CommitteeRole> commRole = new ArrayList<CommitteeRole>();
		
		String createdBy;
		for(int i = 0; i < commTable.size(); i++)
		{	
			StringBuffer sb = new StringBuffer();
			out.print("<tr id=\"commTable2\" class='"+commTable.get(i).getCreatedBy()+"'>");
			out.print("<td width=\"15%\">");
			out.print(commTable.get(i).getTitle());
			out.print("</td>");

			out.print("<td width=\"25%\">");
			out.print(commTable.get(i).getPurpose());
			out.print("</td>");

			commRole = Database.CommitteeRoleDB.getCommitteeRole(commTable.get(
					i).getId());
			out.print("<td width=\"15%\">");
			for (int j = 0; j < commRole.size(); j++) {
				out.print(commRole.get(j).getName() + "&nbsp-&nbsp");
				String[] arr = commRole.get(j).getStaffName().split(", ");
				for (int k = 0; k < arr.length; k++) {
					if (k == arr.length - 1) {
						out.print(Database.StaffDB.getStaffNamebyId(arr[k]));
					} else {
						out.print(Database.StaffDB.getStaffNamebyId(arr[k])
								+ ", ");
					}
					
				}
				
				out.print("<br>");
			}
			if (userrole.equals("1")) {
				request.setAttribute("commListRow", commRole.size());
				System.out.println("Committee id : " + commTable.get(i).getId());
				/*sb.append("<a href=\"committee_newcomm2.jsp?action=e&id=");
				sb.append(commTable.get(i).getId()).append("\">Add staff</a><br>");
				System.out.println(sb.toString());
				out.print(sb.toString()); */
			}
			out.print("</td>");
			
			out.print("<td width=\"15%\">");
			
			out.print(formatdate(commTable.get(i).getDate()));
			
			out.print("</td>");
			createdBy = StaffDB.getStaffNamebyId((String) commTable.get(i).getCreatedBy());
			out.print("<td>");
			out.print(createdBy);
			out.print("</td>");
				

				
//				out.print("<td>");
//				out.print("<a href='staffapp_approve.jsp?id=" + apptTable.get(i).getMd5() + "'>Choose...</a>");
//				out.print("</td>");
			out.print("</tr>");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private String formatdate(String date) {
		String result = date.split(" ")[0] + " " + date.split(" ")[1].charAt(0)
				+ date.split(" ")[1].substring(1).toLowerCase() + " "
				+ date.split(" ")[2];
		return result;
	}
}
